var beanstalkc = require('./beanstalk_client');
var randomstring = require("randomstring");

var server = '127.0.0.1:11300'
var start = Date.now()
var total = 0

var TEST_STRING_LEN = 100

var TEST_STRING = randomstring.generate(TEST_STRING_LEN,true);

beanstalkc.connect(server, function(err, conn) {
	if(err){
		console.log(`connect error ${err}`)
		return;
	}

	var onCall = function(err,job_id,data) {
		total = total+1
		if(total>=100000){
			console.log(`run finish total ${total} cost ${Date.now() - start} ms`)
			return
		}
		
		if(err){
			console.log(`call error ${err}`);
		}
		var job_data = TEST_STRING;
		conn.call(0, 5, 3, job_data,onCall);
	}

	var job_data = randomstring.generate(TEST_STRING_LEN,true);
	conn.call(0, 5, 3, job_data,onCall);

});


beanstalkc.connect(server, function(err, conn) {
	if(err){
		console.log(`connect error ${err}`)
		return;
	}
	var onReserve = function(err, job_id, job_json){
		if(err){
			console.log(`reserve error ${err}`);
			conn.reserve(onReserve)
			return
		}
		
		
		//console.log(`got id:${job_id} data:${job_json}`);
		var reply_data = TEST_STRING;
		
		conn.reply(job_id,reply_data,function(err){
			//console.log(`replied:${err}`);
			conn.reserve(onReserve)
		});
	}
	
	conn.reserve(onReserve);
});
